import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import '../assets/css/global.css'
import Home from '../components/home.vue'
import WelCome from '../components/WelCome.vue'
import User from '../components/User/user.vue'
import Rights from '../components/Power/Rights.vue'
import Roles from '../components/Roles/Roles.vue'
import Goods from '../components/Goods/goods_cate.vue'
import Params from '../components/Goods/goods_params.vue'
import GoodsList from '../components/Goods/goodsList.vue'
import addGoods from '../components/Goods/addGoods.vue'
import Order from '../components/Order/order.vue'
import Reports from '../components/report/Report.vue'

Vue.use(VueRouter)
/*eslint-disable*/  

// children 表示子路由（资源路径）
// redirect 跳转
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/home', component: Home,
  // // 重定向 redirect
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: WelCome
      },
      {
        path: '/users',
        component: User
      },
      { path:'/rights',
        component: Rights
      },
      { path: '/roles',
      component: Roles
      },
      { path: '/categories',
      component: Goods
      },{ path: '/params',
      component: Params
      },{
        path:'/goods',
        component: GoodsList
      },{
        path:'/goods/add',
        component: addGoods
      },{
        path: '/orders',
        component: Order
      },{
        path:'/reports',
        component: Reports
      }
    ]
    }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫  判断是否有进行登录
router.beforeEach((to,from,next) => {
  /* to 将要去的路径
     from 从哪李来
     next 一个函数，表示放行
     next() 放行  next('/login') 强制跳转*/
  
    if (to.path === '/login') return next();
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
  })
export default router
